package com.smartleanx.module.hrm.service.esign.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.mybatis.core.dataobject.BaseDO;
import com.smartleanx.module.hrm.controller.admin.esign.archiving.param.HrmEContractArchivingParam;
import com.smartleanx.module.hrm.controller.admin.esign.archiving.result.HrmContractArchivingResult;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmEContractArchivingMapper;
import com.smartleanx.module.hrm.service.esign.service.HrmEContractArchivingService;
import com.smartleanx.module.hrm.service.esign.service.HrmEFolderArchivingService;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEContractArchiving;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEContractTemp;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEFolderArchiving;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEFolderTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * e签宝合同档案Service实现
 *
 * @author xzq
 * @since 2025-07-31 13:35:48
 */
@Service
public class HrmEContractArchivingServiceImpl extends ServiceImpl<HrmEContractArchivingMapper, HrmEContractArchiving> implements HrmEContractArchivingService {

    @Autowired
    private HrmEFolderArchivingService hrmEFolderArchivingService;

    @Override
    public PageResult<HrmContractArchivingResult> getArchivingPage(HrmEContractArchivingParam param) {
        List<Long> archivingIds = null;
        if(param.getFolderId() != null){
            List<HrmEFolderArchiving> list = hrmEFolderArchivingService.getFolderListByFolderId(param.getFolderId());
            archivingIds = list.stream().map(HrmEFolderArchiving::getArchivingId).collect(Collectors.toList());
            if(CollUtil.isEmpty(archivingIds)){
                return new PageResult<>(new ArrayList<>(),0L);
            }
        }

        LambdaQueryWrapper<HrmEContractArchiving> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HrmEContractArchiving::getDataStatus,1);
        queryWrapper.eq(StrUtil.isNotBlank(param.getContractName()),HrmEContractArchiving::getContractName,param.getContractName());
        queryWrapper.eq(StrUtil.isNotBlank(param.getContractCode()),HrmEContractArchiving::getContractNo,param.getContractCode());
        queryWrapper.like(StrUtil.isNotBlank(param.getActor()),HrmEContractArchiving::getActorName,param.getActor());
        queryWrapper.eq(StrUtil.isNotBlank(param.getCreateUser()),HrmEContractArchiving::getCreateName,param.getCreateUser());
        if(CollUtil.isNotEmpty(archivingIds)){
            queryWrapper.in(HrmEContractArchiving::getId,archivingIds);
        }
        if(param.getCreateTime()!=null && param.getCreateTime().length>0){
            queryWrapper.between(HrmEContractArchiving::getStartTime,param.getCreateTime()[0],param.getCreateTime()[1]);
//            queryWrapper.lt(HrmEContractArchiving::getCreateTime,param.getCreateTime()[0]);
//            queryWrapper.gt(HrmEContractArchiving::getCreateTime,param.getCreateTime()[1]);
        }
        if(param.getSignEndTime()!=null && param.getSignEndTime().length>0){
            queryWrapper.between(HrmEContractArchiving::getSignEndTime,param.getSignEndTime()[0],param.getSignEndTime()[1]);
//            queryWrapper.lt(HrmEContractArchiving::getSignEndTime,param.getSignEndTime()[0]);
//            queryWrapper.gt(HrmEContractArchiving::getSignEndTime,param.getSignEndTime()[1]);
        }

        List<HrmEContractArchiving> list = list(queryWrapper);

        List<HrmContractArchivingResult> result = list.stream().map(p -> {
            HrmContractArchivingResult hrmContractArchivingResult = new HrmContractArchivingResult();
            BeanUtil.copyProperties(p, hrmContractArchivingResult);
            hrmContractArchivingResult.setContractCode(p.getContractNo());
            hrmContractArchivingResult.setStartTime(DateUtil.format(p.getStartTime(), DatePattern.NORM_DATETIME_PATTERN));
            hrmContractArchivingResult.setSignEndTime(DateUtil.format(p.getSignEndTime(), DatePattern.NORM_DATETIME_PATTERN));
            return hrmContractArchivingResult;
        }).collect(Collectors.toList());

        long count = count(queryWrapper);

        return new PageResult<>(result,count);
    }
}
